
import jieba
from wordcloud import WordCloud
import re

from PIL import Image

import matplotlib.pyplot as plt

def read_file_gbk(filename):
    with open(filename,'r',encoding='GBK') as f:
        s = f.read()
        s = re.sub('/C', '', s)
        s = re.sub('\r|\n|\s','',s)
    return s
import jieba
import numpy as np



#统计形状
def calculate_shape(text1,text2):
    #  读取文本，输出为长串字符
    s = read_file_gbk(text1)

    sentences = re.split(r' *[\.\？！，。…… —— oo ll 99][\'"\)\]]* *', s)

    SIZE_AA_ZZ = 0;
    SIZE_AABB_ZZ = 0;
    SIZE_ABB_ZZ = 0;
    SIZE_ABAB_ZZ = 0;
    for stuff in sentences:
        # print(stuff)
        # 原理解析
        # "(.)\1(.)\2"这个正则，
        # .表示除换行外任意字符
        # \1 表示第一个括号里面的字符重复，默认重复一次，想重复4次加{4} 即(.)\1{4}
        # \2 表示第二个括号里面的字符重复
        # 开始匹配AA
        strings = re.finditer(r'(.)\1', stuff)

        # print(type(strings))
        for i in strings:

            SIZE_AA_ZZ = SIZE_AA_ZZ + 1
        strings = re.finditer(r'(.)\1(.)\2', stuff)
        for i in strings:

            SIZE_AABB_ZZ = SIZE_AABB_ZZ + 1
        strings = re.finditer(r'(.)\1(.)\2', stuff)
        for i in strings:

            SIZE_ABB_ZZ = SIZE_ABB_ZZ + 1
        strings = re.finditer(r'(..)\1', stuff)
        for i in strings:

            SIZE_ABAB_ZZ = SIZE_ABAB_ZZ + 1

    s = read_file_gbk(text2)

    sentences = re.split(r' *[\.\？！，。…… —— oo ll 99 ００ ４４ ６６ １１ 一一 tt // ww][\'"\)\]]* *', s)

    SIZE_AA_QQ = 0
    SIZE_AABB_QQ = 0
    SIZE_ABB_QQ = 0
    SIZE_ABAB_QQ = 0
    for stuff in sentences:

        strings = re.finditer(r'(.)\1', stuff)

        for i in strings:

            SIZE_AA_QQ = SIZE_AA_QQ + 1
        strings = re.finditer(r'(.)\1(.)\2', stuff)
        for i in strings:

            SIZE_AABB_QQ = SIZE_AABB_QQ + 1
        strings = re.finditer(r'(.)\1(.)\2', stuff)
        for i in strings:

            SIZE_ABB_QQ = SIZE_ABB_QQ + 1
        strings = re.finditer(r'(..)\1', stuff)
        for i in strings:

            SIZE_ABAB_QQ = SIZE_ABAB_QQ + 1

    # start draw

    # 构建数据
    x_data = ['AA', 'AABB', 'ABB', 'ABAB']
    y_data = [SIZE_AA_ZZ, SIZE_AABB_ZZ, SIZE_ABB_ZZ, SIZE_ABAB_ZZ]
    y_data2 = [SIZE_AA_QQ, SIZE_AABB_QQ, SIZE_ABB_QQ, SIZE_ABAB_QQ]
    bar_width = 0.3

    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
    # 将X轴数据改为使用range(len(x_data), 就是0、1、2...
    plt.bar(x=x_data, height=y_data, label='修仙小说',
            color='steelblue', alpha=0.8, width=bar_width)
    # 将X轴数据改为使用np.arange(len(x_data))+bar_width,
    # 就是bar_width、1+bar_width、2+bar_width...这样就和第一个柱状图并列了
    plt.bar(x=np.arange(len(x_data)) + bar_width, height=y_data2,
            label='言情小说', color='indianred', alpha=0.8, width=bar_width)
    # 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式
    for x, y in enumerate(y_data):
        plt.text(x, y + 100, '%s' % y, ha='center', va='bottom')
    for x, y in enumerate(y_data2):
        plt.text(x + bar_width, y + 100, '%s' % y, ha='center', va='top')
    # 设置标题
    plt.title("修仙小说与言情小说词形对比")
    # 为两条坐标轴设置名称
    plt.xlabel("类型")
    plt.ylabel("数量")
    # 显示图例
    plt.legend()
    plt.show()


calculate_shape('XX.txt','YQ.txt')




